package com.dameng.jianyouquan.mvp.view.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.Nullable;

import com.dameng.jianyouquan.R;
import com.dameng.jianyouquan.api.Constant;
import com.dameng.jianyouquan.base.mvpBase.BaseMvpActivity;
import com.dameng.jianyouquan.base.mvpBase.BasePresenter;
import com.dameng.jianyouquan.mvp.model.LoginModel;
import com.dameng.jianyouquan.mvp.presenter.impl.LoginPresenterImpl;
import com.dameng.jianyouquan.mvp.view.view.LoginView;
import com.dameng.jianyouquan.utils.SpUtils;

import java.util.HashMap;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.sharesdk.framework.Platform;
import cn.sharesdk.framework.PlatformActionListener;
import cn.sharesdk.framework.ShareSDK;
import cn.sharesdk.tencent.qq.QQ;
import cn.sharesdk.wechat.friends.Wechat;

public class LoginActivity extends BaseMvpActivity implements LoginView {


    @BindView(R.id.et_login_phoneNum)
    EditText etLoginPhoneNum;
    @BindView(R.id.et_login_pwd)
    EditText etLoginPwd;
    @BindView(R.id.btn_login)
    Button btnLogin;
    @BindView(R.id.login_tv_register)
    TextView loginBtnRegister;
    @BindView(R.id.tv_login_forgetPwd)
    TextView tvLoginForgetPwd;
    @BindView(R.id.rl_login_qq)
    RelativeLayout rlLoginQq;
    @BindView(R.id.rl_login_weChat)
    RelativeLayout rlLoginWeChat;
    private LoginPresenterImpl mPresenter;
    PlatformActionListener platformActionListener;

    private String strPhoneNum="";
    private String strPwd="";

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        ButterKnife.bind(this);

        doLoginBtnClickAble();

    }


    @Override
    protected boolean isUseFullScreenMode() {
        return true;
    }

    @Override
    protected void initData() {

        platformActionListener=new PlatformActionListener() {
            @Override
            public void onComplete(Platform platform, int i, HashMap<String, Object> hashMap) {
                showToast("登录成功");
            }

            @Override
            public void onError(Platform platform, int i, Throwable throwable) {
                showToast(throwable.getMessage()+"");
            }

            @Override
            public void onCancel(Platform platform, int i) {
                showToast("cancel");
            }
        };



    }

    private void doLoginBtnClickAble() {
        etLoginPhoneNum.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                strPhoneNum=charSequence.toString().trim();
                if (!TextUtils.isEmpty(strPhoneNum)&&!TextUtils.isEmpty(strPwd)){
                    btnLogin.setClickable(true);
                    btnLogin.setBackgroundResource(R.drawable.circle_login_bg_click);
                }else {
                    btnLogin.setClickable(false);
                    btnLogin.setBackgroundResource(R.drawable.circle_login_bg_unclick);
                }
            }

            @Override
            public void afterTextChanged(Editable editable) {

            }
        });


        etLoginPwd.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                strPwd=charSequence.toString().trim();
                if (!TextUtils.isEmpty(strPhoneNum)&&!TextUtils.isEmpty(strPwd)){
                    btnLogin.setClickable(true);
                    btnLogin.setBackgroundResource(R.drawable.circle_login_bg_click);
                }else {
                    btnLogin.setClickable(false);
                    btnLogin.setBackgroundResource(R.drawable.circle_login_bg_unclick);
                }
            }

            @Override
            public void afterTextChanged(Editable editable) {


            }
        });
    }

    @Override
    protected BasePresenter createPresenter() {
        mPresenter = new LoginPresenterImpl(this, new LoginModel());
        return mPresenter;
    }


    @Override
    public void loginSuccess() {
        showToast("登录成功");
        SpUtils.setValueInt(getApplicationContext(), Constant.SP_STATUS,1);
        startActivity(new Intent(LoginActivity.this, ChooseIdentityActivity.class));
    }

    @Override
    public void loginFail(String msg) {
        showToast(msg);
    }



    @OnClick({R.id.btn_login, R.id.login_tv_register, R.id.tv_login_forgetPwd,R.id.rl_login_qq, R.id.rl_login_weChat})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn_login:
                mPresenter.login(getApplicationContext(),etLoginPhoneNum.getText().toString().trim(), etLoginPwd.getText().toString().trim());
                break;
            case R.id.login_tv_register:
                startActivity(new Intent(LoginActivity.this, RegisterActivity.class));
                break;
            case R.id.tv_login_forgetPwd:
                startActivity(new Intent(LoginActivity.this, ForgetPwdActivity.class));
                break;
            case R.id.rl_login_qq:
                Platform plat = ShareSDK.getPlatform(QQ.NAME);
                plat.removeAccount(true);
                plat.SSOSetting(false);
                plat.setPlatformActionListener(platformActionListener);
                plat.showUser(null);
                break;
            case R.id.rl_login_weChat:
                Platform plat2 = ShareSDK.getPlatform(Wechat.NAME);
                plat2.removeAccount(true);
                plat2.SSOSetting(false);
                plat2.setPlatformActionListener(platformActionListener);
                plat2.showUser(null);
                break;
        }
    }

}
